<template>
  <el-main>
    <el-table  v-show="flag"
      v-loading="loading"
      :data="tableData2_0"
      ref="tableData2_0"
      :span-method="objectSpanMethod"
      style="width: 100%"
      border
    >
      <el-table-column
        prop="typeName"
        label="资料种类"
        header-align="center"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="itemName"
        label="资料类型"
        header-align="center"
        align="center"
      >
      </el-table-column>
      <el-table-column prop="unit" label="单位" header-align="center"
        align="center"> </el-table-column>
      <el-table-column prop="receiveNum" label="数量" header-align="center" align="center">
      </el-table-column>
      <el-table-column prop="dataSize" label="数据量（MB）" header-align="center" align="center">
      </el-table-column>
      <el-table-column prop="amount" label="金额（元）" header-align="center" align="center">
      </el-table-column>
    </el-table>
    
    <el-dialog width="80%" v-on="$listeners" :visible.sync="openChartDialog"  title="月度对比统计图">
      <div class="el-dialog-div" style="height: 72vh; overflow-y: scroll">
        <monthcompare ref="monthcompare"></monthcompare>
      </div>
    </el-dialog>
  </el-main>
</template>

<script>
import { deptMonthReport,getLastDayOfMonth} from "@/api/report/basic";
import monthcompare from '../dialog/monthcompare'


export default {
  name: "table2_0",
  components:{
    monthcompare,
  },
  data() {
    return {
      spanArr: [],
      flag: true,
      loading: false,
      tableData2_0: [],
      openChartDialog: false,
    };
  },
  mounted: function () {
  //  this.getReportData();
    this.openChartDialog = false;
    this.$emit("reportSearch","");
  },
  methods:{
    getSpanArr(data) {
      this.spanArr = [];
      this.pos = 0;
      for (var i = 0; i < data.length; i++) {
        if (i === 0) {
          this.spanArr.push(1);
          this.pos = 0;
        } else {
          if (data[i].typeName === data[i - 1].typeName) {
            this.spanArr[this.pos] += 1;
            this.spanArr.push(0);
          } else {
            this.spanArr.push(1);
            this.pos = i;
          }
        }
      }
    },
    objectSpanMethod({ rowIndex, columnIndex }) {
      if (columnIndex === 0) {
        const _row = this.spanArr[rowIndex];
        const _col = _row > 0 ? 1 : 0;
        return {
          rowspan: _row,
          colspan: _col
        }
      }
    },
    export2Excel(name) {
        const tHeader = ["资料种类", "资料类型", "单位","数量","数据量（MB）","金额（元）"];
        var filterVal = []
        for(var i in this.tableData2_0[0]){
          if((i != "typeCode")&&(i != "issuesType")&&
             (i != "id")&&
             (i != "dataType")&&(i != "mapScale")&&
             (i != "sequence")&&(i != "supplyWay")){
            filterVal.push(i)
          }
        }
        console.log(filterVal)
       // common.export2Excel(name,tHeader,filterVal,this.tableData0_0)
        require.ensure([], () => {
          const { export_json_to_excel } = require("@/views/basic-report/common/Export1_5");
          const list = this.tableData2_0;
          const data = this.formatJson(filterVal, list);
          
          export_json_to_excel(tHeader, data, name);
        });
    },
    showchart(selectValue,queryParams){
       this.openChartDialog = true;

    },
    async initData(data) {
          //获取当前时间
          var now   = new Date();
          var monthn = now.getMonth()+1;
          var yearn  = now.getFullYear();
          this.selectMonth = yearn+"-"+monthn;
        },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]));
    },
               getReportData(queryParams) {
                   this.loading = true;
                   var condition = {};
                   condition.beginTime = queryParams.selectMonth+"-01";
                   condition.endTime = getLastDayOfMonth(queryParams.selectMonth);
                   deptMonthReport(condition).then(response => {
                              this.tableData2_0 = response.rows;
                              this.total = response.total;
                              this.getSpanArr(this.tableData2_0);
                              this.loading = false;
                     });
                 }
  }
};
</script>
